/**
 * Displays the XML Code for the test set currently being edited / created. 
 */

FBL.ns(function()
{
    with ( FBL )
    {

        function TestFireAgentPanel()
        {
        }
        TestFireAgentPanel.prototype = extend(Firebug.Panel,
        {
            name: "TestFireAgentPanel",
            title: "Agent",
            searchable: false,
            editable: false,
            parentPanel: "TestFireTreePanel",


            saveToAgent: function( form )
            {

                var baseUrl = Firebug.TestFire.settings.testFireAgentUrl + "/";
                var opts = {};
                var fcn = function( httpRequest )
                {
                    alert('Save Completed');
                    this._refresh();
                }
                opts.onSuccess = Firebug.TestFire.util.bindFixed(fcn, this );

                fcn = function( httpRequest, ex )
                {
                    alert('onFailure');
                    this.setAgentResponseText('There was a problem with the request: ' + httpRequest.responseText);
                }
                opts.onFailure = Firebug.TestFire.util.bindFixed(fcn,this );

                fcn = function( httpRequest, ex )
                {
                    alert('onExe');
                    this.setAgentResponseText('There was a problem with the request: ' + ex);
                }
                opts.onException = Firebug.TestFire.util.bindFixed(fcn, this);

                Firebug.TestFire.util.formRequest(form, opts, baseUrl + form.getAttribute('action'));

                return true;
            }
            ,

            setAgentResponseText: function( text )
            {
                var div = this.panelNode.ownerDocument.getElementById('agentPanelContent');
                div.innerHTML = text;

            }
            ,

            initializeNode: function()
            {
                try
                {
                    var div = this.panelNode.ownerDocument.getElementById('agentPanelContent');
                    var url = Firebug.TestFire.settings.testFireAgentUrl;
                    url = url + "/submit_xml.jsp";
                    if ( !div )
                    {
                        div = this.panelNode.ownerDocument.createElement('div');
                        div.id = 'header_div';
                        div.innerHTML = "<h4>Contacting " + url + " : </h4>";
                        this.panelNode.appendChild(div);
                        div = this.panelNode.ownerDocument.createElement('div');
                        div.id = 'agentPanelContent';
                        this.panelNode.appendChild(div);

                    }
                    var opts = {};
                    var fcn = function( httpRequest )
                    {
                        this.handleInitResponse(httpRequest.responseText);
                    };
                    opts.onSuccess = Firebug.TestFire.util.bindFixed(fcn, this)

                    fcn = function( httpRequest )
                    {
                        this.handleInitResponse('<h3>There was a problem with the request: </h3><p>' +
                                                httpRequest.responseText + "</p>");
                    }
                    opts.onFailure = Firebug.TestFire.util.bindFixed(fcn, this)
                    fcn = function( url )
                    {
                        this.handleInitResponse(url
                            + ' is not available, please verify that the agent is running and try again.');
                    };

                    opts.onException = Firebug.TestFire.util.bindFixed(fcn, this, url)
                    opts.method = 'get';
                    opts.parameters = {noform: true};
                    Firebug.TestFire.util.request(url, opts);
                    return true;
                }
                catch
                    ( ex )
                {
                    alert(url + ' is not available.');

                }

            }
            ,

            handleInitResponse: function ( responseText )
            {
                var div = this.panelNode.ownerDocument.getElementById('agentPanelContent');
                var form = null;



                div.innerHTML = responseText;
                var xmlTA = div.ownerDocument.getElementById("testset_xml");
                if ( xmlTA )
                {
                    xmlTA.value = FirebugContext.getPanel("TestFireCodePanel").asString;
                }
                var btn = div.ownerDocument.getElementById("submit");
                form = div.ownerDocument.forms[0];

                if ( btn )
                {
                    btn.onclick = Firebug.TestFire.util.bindFixed(this.saveToAgent, this, form);
                }

            }
            ,

            showPanel: function( browser, panel )
            {
                alert('showing testfireagentpanel');

            }
            ,

            refresh: function()
            {
                alert('refreshing testfireagentpanel');
            }
            ,

            supportsObject: function( object )
            {
                return false;
            }
            ,

            copyTextAreaToClipboard: function()
            {
                var textField = this.panelNode.ownerDocument.getElementById('theTextContent');
                var text = textField.value;
                copyToClipboard(text); // Firebug function.

            }
            ,

            getContextMenuItems: function( node, target )
            {
                try
                {
                    var items = [];
                    items.push(
                        "-",
                    {nol10n:1, label: "Copy To Clipboard", command: Firebug.TestFire.util.bindFixed(this.copyTextAreaToClipboard, this) },
                    {nol10n:1, label: "Refresh", command: Firebug.TestFire.util.bindFixed(this._refresh, this) }
                        );
                }
                catch( ex )
                {
                    Firebug.TestFire.util.tf_debug(ex);
                }

                return items;
            }
            ,

            _refresh: function()
            {
                this.initializeNode();
            }

        });

        Firebug.registerPanel(TestFireAgentPanel);

    }
})
    ;
